<?php

namespace teach\controllers;

use teach\service\Calculation;
use teach\service\CalculationAssemble;
use teach\util\helper\ResponseHelper;
use Yii;
use yii\filters\Cors;
use yii\rest\ActiveController;
use yii\web\Response;

class ShellController extends ActiveController
{
    public $modelClass = '';

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        $behaviors['corsFilter'] = [
            'class' => Cors::class,
            'cors' => [
                'Origin' => ['*'],
                'Access-Control-Request-Method' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
                'Access-Control-Request-Headers' => ['*'],
                'Access-Control-Allow-Credentials' => true,
                'Access-Control-Max-Age' => 86400,
            ],
        ];
        return $behaviors;
    }

    /**
     * 每天的执行脚本
     * @return Response
     */
    public function actionOrder()
    {
        $request = Yii::$app->request;
        $oid = $request->post('oid');
        $uid = $request->post('uid');
        $mid = $request->post('mid');
        $out_trade_no = $request->post('out_trade_no');

        $msg = '';
        $flag = false;
        if (!$oid) {
            $msg = 'oid 不能为空';
        } elseif (!$uid) {
            $msg = 'uid 不能为空';
        } elseif (!$mid) {
            $msg = 'mid 不能为空';
        } elseif (!$out_trade_no) {
            $msg = 'out_trade_no 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $calculation = new Calculation();
            $res = $calculation->add($oid, $uid, $mid, $out_trade_no);
            //$time = date('Y-m-d');
            //file_put_contents("./calculation/{$time}.txt" ,$oid . "-" . $uid . "-" . $mid . "-". $out_trade_no ."\n",FILE_APPEND);
            if ($res) {
                $jsonData = ResponseHelper::buildJsonData(200, '成功');
            } else {
                $jsonData = ResponseHelper::buildJsonData(400, '失败');
            }
        }
        return $this->asJson($jsonData);
    }

    /**
     * 每天的执行拼团订单脚本
     * @return Response
     */
    public function actionOrders()
    {
        $request = Yii::$app->request;
        $oid = $request->post('oid');
        $uid = $request->post('uid');
        $mid = $request->post('mid');
        $out_trade_no = $request->post('out_trade_no');

        $msg = '';
        $flag = false;
        if (!$oid) {
            $msg = 'oid 不能为空';
        } elseif (!$uid) {
            $msg = 'uid 不能为空';
        } elseif (!$mid) {
            $msg = 'mid 不能为空';
        } elseif (!$out_trade_no) {
            $msg = 'out_trade_no 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $calculationAssemble = new CalculationAssemble();
            $res = $calculationAssemble->add($oid, $uid, $mid, $out_trade_no);
            //$time = date('Y-m-d');
            //file_put_contents("./calculation/{$time}.txt" ,$oid . "-" . $uid . "-" . $mid . "-". $out_trade_no ."\n",FILE_APPEND);
            if ($res) {
                $jsonData = ResponseHelper::buildJsonData(200, '成功');
            } else {
                $jsonData = ResponseHelper::buildJsonData(400, '失败');
            }
        }
        return $this->asJson($jsonData);
    }
}